package com.neusoft.neu.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.neusoft.neu.mapper.MemberMapper;
import com.neusoft.neu.service.IMemberService;
import com.neusoft.neu24.Result.CommonResult;
import com.neusoft.neu24.Result.ResultCode;
import com.neusoft.neu24.dto.GridorDTO;
import com.neusoft.neu24.dto.MemberDTO;
import com.neusoft.neu24.dto.UserLoginDTO;
import com.neusoft.neu24.entity.Grid;
import com.neusoft.neu24.entity.Gridor;
import com.neusoft.neu24.entity.Member;


import com.neusoft.neu24.service.impl.GridServiceImpl;
import io.swagger.models.auth.In;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements IMemberService {

    @Resource
    private MemberMapper memberMapper;



    @Override
    public CommonResult<Member> login(UserLoginDTO userLoginDTO) {
        Member m = new Member();
        return CommonResult.success(m);
    }

    @Override
    public CommonResult<Member> loginWithPhone(UserLoginDTO userLoginDTO) {
        Member m = null;
        //校验手机格式
        if (!checkPhoneRegex(userLoginDTO.getTel())) return CommonResult.failed(ResultCode.LOGIN_PHONE_ILLEGAL);
        //检查验证码
        if (!checkSMSCode(userLoginDTO.getCode())) return CommonResult.failed(ResultCode.LOGIN_FAILED_CODEERROR_TIMEOUT);
        //检查用户手机号是否已存在
        //未存在
        if (!checkPhone(userLoginDTO.getTel())){
            m = createNewMember(userLoginDTO.getTel());
        }//已存在
        else{
            m = memberMapper.selectMemberByPhoneLogin(userLoginDTO);
        }
        System.out.println(m);

        return CommonResult.success(m);
    }

    @Override
    public CommonResult<List<MemberDTO>> listAll() {
        List<MemberDTO> members = memberMapper.listAll();
        return CommonResult.success(members);
    }

    @Override
    public CommonResult<Integer> newMember() {
        return CommonResult.success(memberMapper.addMember(createNewMember("123")));
    }

    @Override
    public CommonResult<Integer> switchMember(Member member) {
        return memberMapper.switchMember(member);
    }

    @Override
    public CommonResult<List<GridorDTO>> getGridors() {
        List<GridorDTO> gridors = memberMapper.getGridors();
        for (GridorDTO g : gridors) {
            if(g.getGid() != null) g.setGids(getPids(g.getGid()).getData());
        }
        System.out.println(gridors.toString());
        memberMapper.insertGridors(gridors);
        return CommonResult.success(gridors);
    }

    @Override
    public CommonResult<Integer> editGridor(Gridor gridor) {
        System.out.println(gridor.toString());
        gridor.setGid(gridor.getGids().get(gridor.getGids().size()-1));

        Integer res;
        if ((res = memberMapper.editGridor(gridor)) == 0) return CommonResult.failed(ResultCode.DATA_ERROR);
        return CommonResult.success(res);
    }

    @Override
    public CommonResult<Integer> addMember(Member member) {
        member.setLogid("p"+member.getTel());
        System.out.println(member);
        Integer res;
        if ((res = memberMapper.addMember(member)) == 0) return CommonResult.failed(ResultCode.DATA_ERROR);
        if (member.getRoleid() == 2)
            if ((res = memberMapper.addGridor(member)) == 0) return CommonResult.failed(ResultCode.DATA_ERROR);
        return CommonResult.success(res);
    }

    private boolean checkPhone(String tel) {
        return true;
    }

    private boolean checkSMSCode(String code) {
        return true;
    }

    private boolean checkPhoneRegex(String tel) {
        return true;
    }

    private Member createNewMember(String tel) {
        Member member = new Member();
        member.setTel("133"+ RandomUtil.randomNumbers(8));
        member.setGender("男");
        member.setMname("用户"+new Date().getDay()+System.currentTimeMillis()%10000);
        member.setRoleid(12);
        member.setLogid("user"+RandomUtil.randomNumbers(6));
        member.setState(1);
        member.setBirth(new Date());
        return member;
    }

    public CommonResult<List<Integer>> getPids(Integer id) {
        List<Integer> ids = memberMapper.getIdsById(id);

        if (ids.isEmpty() || ids == null) return CommonResult.failed(ResultCode.DATA_ERROR);

        return CommonResult.success(ids);
    }

}
